<template>
  <div>
    <el-upload
      action="http://store-maxlen.oss-cn-guangzhou.aliyuncs.com"
      :data="dataObj"
      list-type="picture-card"
      :file-list="fileList"
      :before-upload="beforeUpload"
      :on-remove="handleRemove"
      :on-success="handleUploadSuccess"
      :on-preview="handlePreview"
      :limit="maxCount"
      :on-exceed="handleExceed"
      :http-request="uploadInput"
      multiple
    >
      <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt />
    </el-dialog>
  </div>
</template>
<script>

import { getUUID } from '@/utils'
import { policy, inputFile, del } from '@/api/third-party/third-party'

export default {
  name: 'MultiUpload',
  props: {
    // 图片属性数组
    value: Array,
    // 最大上传图片数量
    maxCount: {
      type: Number,
      default: 30
    }
  },
  data() {
    return {
      dataObj: {
        policy: '',
        signature: '',
        key: '',
        ossaccessKeyId: '',
        dir: '',
        host: '',
        uuid: ''
      },
      dialogVisible: false,
      dialogImageUrl: null
    };
  },
  computed: {
    fileList() {
      const fileList = []
      for (let i = 0; i < this.value.length; i++) {
        fileList.push({ url: this.value[i] })
      }
      return fileList
    }
  },
  mounted() {},
  methods: {
    emitInput(fileList) {
      const value = []
      for (let i = 0; i < fileList.length; i++) {
        value.push(fileList[i].url)
      }
      this.$emit('input', value)
    },
    handleRemove(file, fileList) {
      this.emitInput(fileList)
    },
    handlePreview(file) {
      this.dialogVisible = true
      this.dialogImageUrl = file.url
    },
    beforeUpload(file) {
      // const _self = this
      // this.$store.dispatch('third-party/third-party/getPolicy')
      //   .then(response => {
      //     console.log('这是什么${filename}')
      //     _self.dataObj.policy = response.data.policy
      //     _self.dataObj.signature = response.data.signature
      //     _self.dataObj.ossaccessKeyId = response.data.accessid
      //     _self.dataObj.key = response.data.dir + '/' + getUUID() + '_${filename}'
      //     _self.dataObj.dir = response.data.dir
      //     _self.dataObj.host = response.data.host
      //   })
      //   .catch(err => {
      //     console.log('出错了...', err)
      //   })
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/jpg'
      const isLt2M = file.size / 1024 / 1024 < 50

      if (!isJPG) {
        this.$message.error('请上传图片!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 50MB!')
      }
      return isJPG && isLt2M
    },
    handleUploadSuccess(file) {
      this.fileList.push({
        name: file.name,
        // url: this.dataObj.host + "/" + this.dataObj.dir + "/" + file.name； 替换${filename}为真正的文件名
        url: this.dataObj.host + '/' + this.dataObj.key.replace('${filename}', file.name)
      })
      this.emitInput(this.fileList)
    },
    handleExceed(files, fileList) {
      this.$message({
        message: '最多只能上传' + this.maxCount + '张图片',
        type: 'warning',
        duration: 1000
      })
    },
    uploadInput(file) {
      policy()
        .then(response => {
          const form = new FormData()
          form.append('OSSAccessKeyId', response.data.accessId)
          const key = response.data.dir + '/' + getUUID() + '_${filename}'
          form.append('key', key)
          form.append('policy', response.data.policy)
          form.append('signature', response.data.signature)
          form.append('file', file.file)

          inputFile(form)
            .then(() => {
              this.dataObj.host = response.data.host
              this.dataObj.key = key
              this.handleUploadSuccess(file.file)
            })
            .catch(() => {
            })
        }).catch(() => {})
    }
  }
}
</script>
<style>
</style>


